package com.work.keep.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.work.keep.R;
import com.work.keep.datas.DynamicDetailsData;
import com.work.keep.entity.dao.CollectionComments;
import com.work.keep.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/22.
 */
public class DynamicDetailsRecyclerAdapter extends RecyclerView.Adapter {
    private static final int TYPE_HEADER = 0, TYPE_ITEM = 1, TYPE_FOOT = 2;
    private View headView;
    private View footView;
    private int headViewSize = 0;
    private int footViewSize = 0;
    //是否有头布局
    private boolean isAddFoot = false;
    //是否有foot布局
    private boolean isAddHead = false;
    private Context context;
    private List<DynamicDetailsData.DataEntity.CommentsListEntity> commentsList;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    //数据库中是否有数据的标志 初始值是false
    private boolean isDBHas = false;
    //数据库中的数据实体类类型
    private List<CollectionComments> collectionCommentses;

    public DynamicDetailsRecyclerAdapter(Context context) {
        this.context = context;
        init();
    }

    public void addDBData(List<CollectionComments> collectionCommentses) {
        isDBHas = true;
        this.collectionCommentses = collectionCommentses;
        notifyDataSetChanged();
    }

    private void init() {
        commentsList = new ArrayList<>();
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
    }

    public void addData(List<DynamicDetailsData.DataEntity.CommentsListEntity> commentsList) {
        this.commentsList = commentsList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE_HEADER:
                view = headView;
                break;

            case TYPE_ITEM:
                view = LayoutInflater.from(context).inflate(R.layout.dynamic_details_comments_item, parent, false);
                return new ItemViewHolder(view);

            case TYPE_FOOT:
                view = footView;
                break;
        }
        return new HeadViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position > headViewSize) {
            if (isDBHas) {
                ((ItemViewHolder) holder).commentsItemName.setText(collectionCommentses.get(position).getUsername());
                ((ItemViewHolder) holder).commentsItemContent.setText(collectionCommentses.get(position).getCotent());

            } else {
                ((ItemViewHolder) holder).commentsItemAvater.setDefaultImageResId(R.mipmap.ic_launcher);
                ((ItemViewHolder) holder).commentsItemAvater.setErrorImageResId(R.mipmap.ic_launcher);
                ((ItemViewHolder) holder).commentsItemAvater.setImageUrl(commentsList.get(position).getAuthor().getAvatar(), imageLoader);
                ((ItemViewHolder) holder).commentsItemName.setText(commentsList.get(position).getAuthor().getUsername());
                ((ItemViewHolder) holder).commentsItemDate.setText(commentsList.get(position).getCreated());
                ((ItemViewHolder) holder).commentsItemContent.setText(commentsList.get(position).getContent());
            }
        }
    }

    public void addHeadView(View view) {
        headView = view;
        headViewSize = 1;
        isAddHead = true;
    }

    @Override
    public int getItemViewType(int position) {
        int type = TYPE_ITEM;

        if (headViewSize == 1 && position == 0) {
            type = TYPE_HEADER;
        } else if (footViewSize == 1 && position == getItemCount() - 1) {
            //最后一个位置
            type = TYPE_FOOT;
        }
        return type;
    }

    public void addFootView(View view) {
        footView = view;
        footViewSize = 1;
        isAddFoot = true;
    }

    @Override
    public int getItemCount() {
        return isDBHas ? collectionCommentses.size() + headViewSize + footViewSize : commentsList.size() + headViewSize + footViewSize;
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        NetworkImageView commentsItemAvater;
        TextView commentsItemName, commentsItemDate, commentsItemContent;

        public ItemViewHolder(View itemView) {
            super(itemView);
            commentsItemAvater = (NetworkImageView) itemView.findViewById(R.id.commentsItemAvater);
            commentsItemName = (TextView) itemView.findViewById(R.id.commentsItemName);
            commentsItemDate = (TextView) itemView.findViewById(R.id.commentsItemDate);
            commentsItemContent = (TextView) itemView.findViewById(R.id.commentsItemContent);
        }
    }

    class HeadViewHolder extends RecyclerView.ViewHolder {

        public HeadViewHolder(View itemView) {
            super(itemView);
        }
    }
}
